package pri.jz.keeper.util

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS: 用于一些Bitmap处理
 */
object BitmapUtil {
    const val TAG = "BitmapUtil"

    //将数据库存储的input文本文件转换为bitmap
    fun getBitmapFromS(text: String): Bitmap? {
        if (text.isBlank()) {
            return null
        }

        val byteArray = text.toByteArray(Charsets.ISO_8859_1)
        return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
    }

    /**
     * 将Bitmap转换为数据库可存储的文本文件
     */
    fun getTextFromBitmap(bitmap: Bitmap?) : String {
        bitmap?.let {
            val baos = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)
            val input = ByteArrayInputStream(baos.toByteArray())
            return String(input.readBytes(),Charsets.ISO_8859_1)
        }
        return ""
    }
}